﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LibraryManagement
{
    public partial class AddBookForm : Form
    {
        private Form parentForm;
        public AddBookForm(Form parent)
        {
            InitializeComponent();
            parentForm = parent;
        }

        //private void btnAddBook_Click(object sender, EventArgs e)
        //{
        //    string isbn = txtISBN.Text;
        //    string title = txtTitle.Text;
        //    string author = txtAuthor.Text;
        //    string category = cmbCategory.SelectedItem.ToString();
        //    string location = txtLocation.Text;
        //    int stock = int.Parse(txtStock.Text);

        //    string query = $"INSERT INTO Books (ISBN, Title, Author, Category, Location, Stock) VALUES ('{isbn}', '{title}', '{author}', '{category}', '{location}', {stock})";
        //    DatabaseHelper.ExecuteNonQuery(query);

        //    MessageBox.Show("图书添加成功！");
        //}

        //private void btnBack_Click(object sender, EventArgs e)
        //{
        //    parentForm.Show();
        //    this.Close();
        //}

        private void btnAddBook_Click_1(object sender, EventArgs e)
        {
            string isbn = txtISBN.Text;
            string title = txtTitle.Text;
            string author = txtAuthor.Text;
            string category = cmbCategory.SelectedItem.ToString();
            string location = txtLocation.Text;
            int stock = int.Parse(txtStock.Text);

            string query = $"INSERT INTO Books (ISBN, Title, Author, Category, Location, Stock) VALUES ('{isbn}', '{title}', '{author}', '{category}', '{location}', {stock})";
            DatabaseHelper.ExecuteNonQuery(query);

            string logQuery = $"INSERT INTO Logs (Action, BookID, Operator, Date) VALUES ('AddBook', {isbn}, '1', '{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}')";
            DatabaseHelper.ExecuteNonQuery(logQuery);

            MessageBox.Show("图书添加成功！");
        }

        private void btnBack_Click_1(object sender, EventArgs e)
        {
            parentForm.Show();
            this.Close();
        }
    }
}
